﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

using Agile;

using Microsoft.AspNetCore.Authorization;

namespace Microsoft.AspNetCore.Mvc
{
    /// <summary>
    /// GetTreeGridJson
    /// GetGridJson
    /// GetFormJson
    /// SubmitForm
    /// DeleteForm
    /// UpdateField
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    [Authorize]
    public class BaseController<TEntity> : BaseController where TEntity : Entity
    {
        /// <summary>
        /// 身份信息
        /// </summary>
        protected ILoginInfo loginInfo { get; set; }

        /// <summary>
        /// cotr
        /// </summary>
        protected BaseController()
        {
            loginInfo = EngineContext.Current.Resolve<ILoginInfo>();
        }

        public IRepository<TEntity> Repository
        {
            get
            {
                var repo = Service<IRepository<TEntity>>();
                return repo;
            }
        }
        #region CURD
        [HttpPost]
        [HttpGet]
        public virtual IActionResult GetTreeJson()
        {
            if (EntityHelper.IsTreeEntity(typeof(TEntity)))
            {
                var data = Repository.GetByAll();
                var treeList = new List<TreeViewModel>();
                for (int i = 0; i < data.Count; i++)
                {
                    TEntity item = data[i];
                    TreeEntity treeEntity = item as TreeEntity;
                    TreeViewModel tree = new TreeViewModel();
                    bool hasChildren = Repository.GetByAll("ParentID='" + item.ID + "'").Count == 0 ? false : true;
                    tree.id = item.ID;
                    tree.text = treeEntity.Name;
                    tree.value = item.ID;
                    tree.parentId = treeEntity.ParentID;
                    tree.isexpand = true;
                    tree.complete = true;
                    tree.checkstate = i == 0 ? 1 : 0;
                    tree.hasChildren = hasChildren;
                    treeList.Add(tree);
                }
                return Content(treeList.TreeViewJson());
            }
            else
            {
                return Content("实体类没有继承TreeEntity类");
            }
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult GetTreeGridJson(string keyword)
        {
            if (EntityHelper.IsTreeEntity(typeof(TEntity)))
            {
                string whereSql = "";
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    whereSql = " Name like '%" + keyword + "%'";
                }
                var data = Repository.GetByAll(whereSql);
                var treeList = new List<TreeGridModel>();
                foreach (TEntity item in data)
                {
                    TreeEntity treeEntity = item as TreeEntity;
                    TreeGridModel treeModel = new TreeGridModel();
                    bool hasChildren = Repository.GetByAll("ParentID='" + item.ID + "'").Count == 0 ? false : true;
                    treeModel.id = item.ID;
                    treeModel.isLeaf = hasChildren;
                    treeModel.parentId = treeEntity.ParentID;
                    treeModel.expanded = hasChildren;
                    treeModel.entityJson = item.ToJson();
                    treeList.Add(treeModel);
                }
                return Content(treeList.TreeGridJson());
            }
            else
            {
                return Content("实体类没有继承TreeEntity类");
            }
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult GetGridJson(string keyword = "")
        {
            var data = Repository.GetByAll(keyword);
            return Content(data.ToJson());
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult GetJGridPageDataJson(Paging paging)
        {
            JQridPageData pageData = Repository.GetJGridPageData(paging);
            return Content(pageData.ToJson());
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult GetFormJson(string keyValue)
        {
            if (string.IsNullOrWhiteSpace(keyValue)) return Error("参数keyValue不能为空！");
            var data = Repository.GetById(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        public virtual IActionResult SubmitForm(TEntity moduleEntity)
        {
            Repository.AddOrUpdate(moduleEntity);
            return Success("提交成功。");
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult DeleteForm(string keyValue)
        {
            if (string.IsNullOrWhiteSpace(keyValue)) return Error("参数keyValue不能为空！");
            Repository.Delete(keyValue);
            return Success("删除成功。");
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult UpdateField(string keyId, string fieldName, string fieldValue)
        {
            if (string.IsNullOrWhiteSpace(keyId)) return Error("参数keyId不能为空！");
            if (string.IsNullOrWhiteSpace(fieldName)) return Error("参数fieldName不能为空！");
            if (string.IsNullOrWhiteSpace(fieldValue)) return Error("参数fieldValue不能为空！");

            Repository.UpdateField(keyId, fieldName, fieldValue);
            return Success("操作成功。");
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult UpdateOrderBy(string keyId, string value)
        {
            Repository.UpdateField(keyId, "OrderBy", value);
            return Success("操作成功。");
        }
        [HttpPost]
        [HttpGet]
        public virtual IActionResult Switch(string menuId)
        {
            var model = Repository.GetById(menuId);
            if (model != null)
            {
                var obj= model.GetPropertyValue("Status");
                if (obj != null)
                {
                    int Status = (int)obj;
                    if (Status > 0)
                    {
                        Status = 0;
                    }
                    else
                    {
                       Status = 1;
                    }
                    model.SetPropertyValue("Status", Status);
                    Repository.Update(model);
                }
            }
            return Success("操作成功。");
        }
        #endregion
    }
}
